*#####################
*  Population density
*#####################

tempfile density asia

import delimited "rawdata/confidential/barometer/API_EN.POP.DNST_DS2_en_csv_v2_4151312.csv", varnames(5) clear

gen 	country_l = .
replace country_l =  1 if countryname == "Japan"
replace country_l =  2 if countryname == "Hong Kong SAR, China" 
replace country_l =  3 if countryname == "Korea, Rep."
replace country_l =  4 if countryname == "China"
replace country_l =  5 if countryname == "Mongolia"
replace country_l =  6 if countryname == "Philippines"
* replace country_l =  7 if countryname == "Taiwan"  # no data
replace country_l =  8 if countryname == "Thailand"
replace country_l =  9 if countryname == "Indonesia"
replace country_l = 10 if countryname == "Singapore"
replace country_l = 11 if countryname == "Vietnam"
replace country_l = 12 if countryname == "Cambodia"
replace country_l = 13 if countryname == "Malaysia"
replace country_l = 14 if countryname == "Myanmar"
replace country_l = 1001 if countryname == "Algeria"  
replace country_l = 1002 if countryname == "Benin"  
replace country_l = 1003 if countryname == "Botswana"  
replace country_l = 1004 if countryname == "Burkina Faso"  
replace country_l = 1005 if countryname == "Burundi"  
replace country_l = 1006 if countryname == "Cameroon"  
replace country_l = 1007 if countryname == "Cabo Verde"  
replace country_l = 1008 if countryname == "Cote d'Ivoire"  
replace country_l = 1009 if countryname == "Egypt, Arab Rep."  
replace country_l = 1010 if countryname == "Gabon"  
replace country_l = 1011 if countryname == "Ghana"  
replace country_l = 1012 if countryname == "Guinea"  
replace country_l = 1013 if countryname == "Kenya"  
replace country_l = 1014 if countryname == "Lesotho"  
replace country_l = 1015 if countryname == "Liberia"  
replace country_l = 1016 if countryname == "Madagascar"  
replace country_l = 1017 if countryname == "Malawi"  
replace country_l = 1018 if countryname == "Mali"  
replace country_l = 1019 if countryname == "Mauritius"  
replace country_l = 1020 if countryname == "Morocco"  
replace country_l = 1021 if countryname == "Mozambique"  
replace country_l = 1022 if countryname == "Namibia"  
replace country_l = 1023 if countryname == "Niger"  
replace country_l = 1024 if countryname == "Nigeria"  
replace country_l = 1025 if countryname == "Sao Tome and Principe"  
replace country_l = 1026 if countryname == "Senegal"  
replace country_l = 1027 if countryname == "Sierra Leone"  
replace country_l = 1028 if countryname == "South Africa"  
replace country_l = 1029 if countryname == "Sudan"  
replace country_l = 1030 if countryname == "Eswatini"  
replace country_l = 1031 if countryname == "Tanzania"  
replace country_l = 1032 if countryname == "Togo"  
replace country_l = 1033 if countryname == "Tunisia"  
replace country_l = 1034 if countryname == "Uganda"  
replace country_l = 1035 if countryname == "Zambia"  
replace country_l = 1036 if countryname == "Zimbabwe"  


* pick up 2017
clonevar density = v62
keep countryname country_l density

keep if country_l!=.



save `density'

*#####################
*  Asian barometer
*#####################

use "rawdata/confidential/barometer/W4_v15_merged20181211_release.dta", clear

sort country

gen postoffice 		= ir12a
gen school 			= ir12b
gen policestation 	= ir12c
gen seweragesystem 	= ir12a
gen healthclinic	= ir12e
gen signalcellphone	= ir12f
gen recreation		= ir12g
gen religion		= ir12h
gen townhall		= ir12i
gen marketstall		= ir12j


bysort country level: gen N =_N
egen sumw = total(w), by(country level)
gen  w_r = N*w/sumw


foreach var of varlist postoffice-marketstall {
	replace `var' = .       if (`var' == -1 | `var' == 9)
	replace `var' = 1 * w_r if (`var' ==  1 | `var' == 3)
	replace `var' = 0 * w_r if (`var' ==  2 | `var' == 4)
}

** with weight(withinwt)


* save asia_r4.dta, replace


* * make copplased data 
* use asia_r4.dta, replace

collapse (mean) postoffice-marketstall, by(country level)

clonevar country_l = country

clonevar urbrur = level 

save `asia', replace




*#####################
*  Afro barometer
*#####################

import spss using  "rawdata/confidential/barometer/merged_r6_data_2016_36countries2.sav",clear

sort COUNTRY

gen postoffice=EA_FAC_A 
gen school=EA_FAC_B
gen policestation=EA_FAC_C
gen healthclinic=EA_FAC_D
gen marketstall=EA_FAC_E
gen bank=EA_FAC_F
gen paidtransport=EA_FAC_G



bysort COUNTRY URBRUR: gen N =_N
egen sumw = total(withinwt), by(COUNTRY URBRUR)
gen  w_r = N*withinwt/sumw




foreach var of varlist postoffice-paidtransport {
	replace `var' = . if (`var' == -1 | `var' == 9)
	replace `var' =  `var' * w_r
}
** with weight(withinwt)



collapse (mean) postoffice-paidtransport, by(COUNTRY URBRUR)

clonevar country_l = COUNTRY
replace country_l = 1000 + country_l

gen 	urbrur = 1 if URBRUR==2
replace urbrur = 2 if URBRUR==1
rename COUNTRY country 
* rename URBRUR urbrur







*#####################
*  Append
*#####################

append using `asia'

merge m:1 country_l using `density', nogen


* labeling 
label define country_l   1 "Japan"   2 "Hong Kong"   3 "Korea"   4 "China"   5 "Mongolia"   6 "Philippines"   7 "Taiwan"   8 "Thailand"   9 "Indonesia"  10 "Singapore"  11 "Vietnam"  12 "Cambodia"  13 "Malaysia"  14 "Myanmar"1001 "Algeria"  1002 "Benin"  1003 "Botswana"  1004 "Burkina Faso"  1005 "Burundi"  1006 "Cameroon"  1007 "Cape Verde"  1008 "Cote d'Ivoire"  1009 "Egypt"  1010 "Gabon"  1011 "Ghana"  1012 "Guinea"  1013 "Kenya"  1014 "Lesotho"  1015 "Liberia"  1016 "Madagascar"  1017 "Malawi"  1018 "Mali"  1019 "Mauritius"  1020 "Morocco"  1021 "Mozambique"  1022 "Namibia"  1023 "Niger"  1024 "Nigeria"  1025 "São Tomé and Príncipe"  1026 "Senegal"  1027 "Sierra Leone"  1028 "South Africa"  1029 "Sudan"  1030 "Swaziland"  1031 "Tanzania"  1032 "Togo"  1033 "Tunisia"  1034 "Uganda"  1035 "Zambia"  1036 "Zimbabwe"  , replace

label values country_l country_l

drop country level  URBRUR countryname
replace urbrur = . if urbrur==-1



label var  density "Population Density (/km2)"
label var  marketstall "Access to Market Stall"
label var  policestation "Access to Police Station"
label var  postoffice "Access to Post Office"
save "intermediate/remoteness_barometer.dta",replace

use "intermediate/remoteness_barometer.dta",clear



keep if urbrur==1

keep country_l density marketstall townhall postoffice policestation
order country_l density marketstall townhall postoffice policestation

* scatter marketstall density if country_l!=5&(marketstall>0.5&density>0.5), msymbol(circle) mcolor(black) mlabel(country_l) ||scatter marketstall density if country_l!=5&(marketstall<0.5|density<50), msymbol(circle) mcolor(black) mlabel(country_l)|| scatter marketstall density if country_l==5, msymbol(diamond) mcolor(black) ylabel(0 0.25 0.5 0.75 1)  mlabel(country_l) legend(off)
scatter marketstall density if country_l!=5, msymbol(circle) mcolor(gs8) mlabel(country_l) mlabcolor(gs8) || scatter marketstall density if country_l==5, msymbol(diamond) mcolor(black) mlabcolor(black) ylabel(0 0.25 0.5 0.75 1) xscale(r(0 700)) mlabel(country_l) legend(off)

graph export  "final/density_marketstall.eps",replace



